package com.example.yyjgit.Controller;

import com.example.yyjgit.Entity.Building;
import com.example.yyjgit.Entity.ResultEntity;
import com.example.yyjgit.Service.BuildingService;
import com.example.yyjgit.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/Building/")
public class BuildingController {
    @Resource
    BuildingService buildingService;
    ResultEntity resultEntity=null;

    //添加建筑
    @PutMapping("addBuilding")
    private String addBuilding (@RequestParam("Name")String Name, @RequestParam("Img") String Img , @RequestParam("Era")String Era, @RequestParam("Classify")String Classify,@RequestParam("Region")String Region){
        resultEntity=new ResultEntity();
        try {
            if (buildingService.addBuilding(Name, Img,Era,Classify,Region) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //删除建筑
    @DeleteMapping("delBuilding")
    private String delBuildingById (@RequestParam("id") int id){
        resultEntity =new ResultEntity();
        try {
            if (buildingService.delBuilding(id)>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return  JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //修改建筑信息
    @PutMapping("upBuilding")
    private String upBuilding(@RequestParam("Name")String Name,@RequestParam("Img") String Img ,@RequestParam("Era")String Era,@RequestParam("Classify")String Classify,@RequestParam("Region")String Region,@RequestParam("id") int id){
        resultEntity=new ResultEntity();
        try{

            if(buildingService.upBuilding(Name,Img,Era,Classify,Region,id) > 0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    //查询建筑
    @GetMapping("getBuilding")
    private String getBuildingById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
            Building building=buildingService.getBuildingById(id);
            if(building==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(building);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

}
